package com.lujianfei.powersearch.utils;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import static android.content.Context.INPUT_METHOD_SERVICE;

public class SystemUtils {

    private static final String TAG = "SystemUtils";

    private static Context applicationContext;

    public static Context getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(Context applicationContext) {
        SystemUtils.applicationContext = applicationContext;
    }

    public static String getVersion(){
        PackageManager packageManager = applicationContext.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(applicationContext.getPackageName(), 0);
            String versionName = packageInfo.versionName;
            return versionName;
        }catch (Exception e){
            Log.e(TAG,""+e);
        }
        return "";
    }


    public static String getAppName(){
        PackageManager packageManager = applicationContext.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(applicationContext.getPackageName(), 0);
            String appName = ResUtils.getString(packageInfo.applicationInfo.labelRes);
            return appName;
        }catch (Exception e){
            Log.e(TAG,""+e);
        }
        return "";
    }

    /**
     * 显示键盘
     *
     * @param et 输入焦点
     */
    public static void showInput(final EditText et) {
        et.postDelayed(new Runnable() {
            @Override
            public void run() {
                et.requestFocus();
                InputMethodManager imm = (InputMethodManager) applicationContext.getSystemService(INPUT_METHOD_SERVICE);
                imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
            }
        },500);
    }

    /**
     * 隐藏键盘
     */
    public static void hideInput(Activity context) {
        final InputMethodManager imm = (InputMethodManager) applicationContext.getSystemService(INPUT_METHOD_SERVICE);
        final View v = context.getWindow().peekDecorView();
        if (null != v) {
            v.post(new Runnable() {
                @Override
                public void run() {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            });
        }
    }

    public static void copy(String text){
        ClipboardManager clipboardManager = (ClipboardManager) applicationContext.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = ClipData.newPlainText("text", text);
        clipboardManager.setPrimaryClip(clipData);
    }

    public static String getPasteContent(){
        ClipboardManager clipboardManager = (ClipboardManager) applicationContext.getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = clipboardManager.getPrimaryClip();
        if (clipData != null && clipData.getItemCount() > 0) {
            return clipData.getItemAt(0).getText().toString();
        }
        return "";
    }
}
